package com.xy.manger;


import com.xy.common.exceptions.ZookeeperException;
import com.xy.common.utils.ZookeeperNode;
import com.xy.common.utils.ZookeeperUtil;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;

import java.util.List;
import java.util.logging.Logger;

/**
 * 注册中心的管理页面
 */
public class MangerApplication {

    private static final Logger logger = Logger.getLogger(MangerApplication.class.getName());

    public static void main(String[] args) {
        // 帮我们创建基础目录
        ZooKeeper zooKeeper = ZookeeperUtil.createZookeeper();

        String bathPath = "/rpc";
        String providersPath = bathPath + "/providers";
        String consumersPath = bathPath + "/consumers";

        ZookeeperNode bathNode = new ZookeeperNode(bathPath, null);
        ZookeeperNode providersNode = new ZookeeperNode(providersPath, null);
        ZookeeperNode consumersNode = new ZookeeperNode(consumersPath, null);

        List.of(bathNode, providersNode, consumersNode).forEach(node -> {
            try {
                if (zooKeeper.exists(node.getNodePath(), null) == null) {
                    zooKeeper.create(node.getNodePath(), node.getData(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                    logger.info("创建节点: " + node.getNodePath() + " 成功");
                }
            } catch (KeeperException | InterruptedException e) {
                logger.info("创建Zookeeper异常" + e);
                throw new ZookeeperException();
            }
        });

    }

}